package com.cssl.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

@Configuration
public class ThreadPool {
    @Bean
    public ExecutorService getPool() {
        Integer max=Runtime.getRuntime().availableProcessors();
        if (max==null){
            max=10;
        }
        ExecutorService threadPoolExecutor=null;
        try {
            threadPoolExecutor  = new ThreadPoolExecutor(
                    8,//核心线程数(不会关闭)
                    max,//最大线程数(当对列中的9个被塞满时会触发)
                    3,//超时时间
                    TimeUnit.SECONDS, //时间单位
                    new LinkedBlockingDeque<>(9), //队列
                    Executors.defaultThreadFactory(), //工厂
                    new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略 (超过 Deque+ maxThread=>哪来的去哪里！)
            );
        }catch (Exception e){
            threadPoolExecutor = Executors.newFixedThreadPool(5);
        }

        return threadPoolExecutor;
    }
}
